var wxValidate = require('../../assets/js/wxValidate.js').wxValidate;
Page({
    data: {
        bannerSwiperConfig: getApp().globalData.bannerSwiperConfig,
        contactInfo: [
            {
                icon:"http://www.gzyueyun.com/m/yueyun-mini-app/assets/img/contact_icon_1.png",
                title:"地址",
                content:"广州市越秀区先烈中路穗丰大厦B座202"
            },
            {
                icon:"http://www.gzyueyun.com/m/yueyun-mini-app/assets/img/contact_icon_2.png",
                title:"免费咨询电话",
                content:"020-37579981"
            },
            {
                icon:"http://www.gzyueyun.com/m/yueyun-mini-app/assets/img/contact_icon_3.png",
                title:"商务合作",
                content:"dengyunjia@gzyueyun.com"
            },
            {
                icon:"http://www.gzyueyun.com/m/yueyun-mini-app/assets/img/contact_icon_4.png",
                title:"工作时间",
                content:"周一~~周五 早上9:00~~下午18:00"
            }
        ],
        contactusinfo: {},
        formData: {
            name: {
                value: "",
                error: false,
                tips: ""
            },
            phone: {
                value: "",
                error: false,
                tips: ""
            },
            email: {
                value: "",
                error: false,
                tips: ""
            },
            content: {
                value: "",
                error: false,
                tips: ""
            }
        },
        formStatus: {
            submitting: false
        },
    },
    onLoad: function () {
        // getApp().getBannerList(this);
        var that = this;
        wx.getStorage({
          key: 'contactusinfo',
          success: function(res) {
              that.setData({
                contactusinfo: res.data
              })
          } 
        })
        this.wxValidate = new wxValidate({
            rules: {
                name: {
                    required: true,
                    minlength: 2,
                    maxlength: 10
                },
                phone: {
                    required: true,
                    mobile: true
                },
                email: {
                    required: true,
                    email: true
                },
                content: {
                    required: true,
                    minlength: 1,
                    maxlength: 120
                }
            },
            tips: {
                name: {
                    required: "必填",
                    minlength: "输入2~10个字符",
                    maxlength: "输入2~10个字符"
                },
                phone: {
                    required: "必填",
                    mobile: "无效的手机号"
                },
                email: {
                    required: "必填",
                    email: "无效的邮箱"
                },
                content: {
                    required: "必填",
                    minlength: "输入1~120个字符",
                    maxlength: "输入1~120个字符"
                }
            }
        });
    },
    // 拨打热线电话
    makePhoneCall: function () {
        wx.makePhoneCall({
            phoneNumber: this.data.contactusinfo.phone
        });
    },
    // 表单数据绑定
    updateFormData: function (e) {
        var name = e.target.dataset.name,
            value = e.detail.value,
            key = "formData." + name + ".value",
            opts = {};
        opts[key] = value;
        this.setData(opts);
    },
    // 更新表单验证结果
    updateErrorData: function (errorData) {
        var key = "formData." + errorData.name,
            opts = {};

        opts[key + '.value'] = errorData.value;
        opts[key + '.error'] = !errorData.valid; // error === !valid
        opts[key + '.tips'] = errorData.tips;

        this.setData(opts);
    },
    tips: function (msg) {
         wx.showToast({
          title: msg,
          icon: 'none',
          duration: 2000
        })
    },
    // 表单验证
    formCheck: function (e) {
        var flag = e.target.dataset.name;
        if(e.detail.value == '' && flag == 'name'){
            this.tips('请输入联系人');
        }else if(e.detail.value == '' && flag == 'phone'){
            this.tips('请输入电话号码');
        }else if(e.detail.value == '' && flag == 'email'){
            this.tips('请输入电话号码');
        }else if(e.detail.value == '' && flag == 'content'){
            this.tips('请输入留言');
        }
        var result = this.wxValidate.formCheck(e);
        if(!result.valid){
            this.tips('请输入正确的信息');
        }
        this.updateErrorData(result);
    },
    // 表单提交
    formSubmit: function (e) {
        var $this = this;
        var formData = e.detail.value;
        var result = this.wxValidate.formCheckAll(formData);
        console.log(formData);
        console.log(result);
        if (result.valid) { // 验证通过
            this.setData({
                "formStatus.submitting": true
            });

            wx.request({
                url: getApp().globalData.url+'/api/Index/message',
                data: formData,
                method: 'post',
                header: {
                    'Content-Type': 'application/x-www-form-urlencoded'
                  },
                success: function (res) {
                    if (res.data.code == 0) {

                         wx.showToast({
                              title: '留言成功',
                              icon: 'success',
                              duration: 2000
                            })
                    } else {

                        wx.showToast({
                              title: '您的填写的格式不真确',
                              icon: 'fail',
                              duration: 2000
                            })

                    }

                },
                complete: function () {

                    $this.setData({
                        "formStatus.submitting": false
                    });

                }
            });

        } else { // 验证不通过

            for (var key in result.detail) {
                this.updateErrorData(result.detail[key]);
            }
            $this.tips('请填写正确的信息');

        }
    },

    wxValidate: null
});